<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('进贡管理列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label> 信士ID：</label>
                            <input type="text" id="personId"  name="personId" th:value="${personId}"/>
                        </li>
                        <li>
                            <label>姓名：</label>
                            <input type="text" name="name"/>
                        </li>

                        <li>
                            <label>金额：</label>
                            <input type="text" name="price"/>
                        </li>
                        <li>
                            <label>进贡商品：</label>
                            <input type="text" name="goods"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="tribute:list:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="tribute:list:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="tribute:list:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="tribute:list:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('tribute:list:edit')}]];
    var removeFlag = [[${@permission.hasPermi('tribute:list:remove')}]];
    var prefix = ctx + "tribute/list";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add?personId="+[[${personId}]],
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            exportUrlInfo: prefix + "/exportInfo",
            modalName: "进贡管理",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '唯一主键',
                    visible: false
                },
                {
                    field: 'name',
                    title: '信士姓名'
                },
                {
                    field: 'phone',
                    title: '信士手机号'
                },

                {
                    field: 'price',
                    title: '金额',
                    formatter: function (value, row, index) {
                        return value + '元\\' + capitalAmount(value);
                    }
                },
                {
                    field: 'goods',
                    title: '进贡商品'
                },
                {
                    field: 'createTime',
                    title: '日期'
                },
                {

                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        actions.push('<a class="btn btn-default btn-xs " href="javascript:void(0)" onclick="exportInfo(\'' + row.id + '\')"><i class="fa fa-print"></i>打印</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function capitalAmount(amount) {
        // 汉字的数字
        const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
        // 基本单位
        const cnIntRadice = ["", "拾", "佰", "仟"];
        // 对应整数部分扩展单位
        const cnIntUnits = ["", "万", "亿", "兆"];
        // 对应小数部分单位
        const cnDecUnits = ["角", "分", "毫", "厘"];
        // 整数金额时后面跟的字符
        const cnInteger = "整";
        // 整型完以后的单位
        const cnIntLast = "元";
        // 最大处理的数字
        const maxNum = 9999999999999999.99;
        // 金额整数部分
        let integerNum;
        // 金额小数部分
        let decimalNum;
        // 输出的中文金额字符串
        let chineseStr = "";
        // 分离金额后用的数组，预定义
        let parts;
        if (amount === "") {
            return "";
        }
        amount = parseFloat(amount);
        if (amount >= maxNum) {
            // 超出最大处理数字
            return "";
        }
        if (amount === 0) {
            chineseStr = cnNums[0] + cnIntLast + cnInteger;
            return chineseStr;
        }
        // 转换为字符串
        amount = amount.toString();
        if (amount.indexOf(".") === -1) {
            integerNum = amount;
            decimalNum = "";
        } else {
            parts = amount.split(".");
            integerNum = parts[0];
            decimalNum = parts[1].substr(0, 4);
        }
        // 获取整型部分转换
        if (parseInt(integerNum, 10) > 0) {
            let zeroCount = 0;
            const IntLen = integerNum.length;
            for (let i = 0; i < IntLen; i++) {
                const n = integerNum.substr(i, 1);
                const p = IntLen - i - 1;
                const q = p / 4;
                const m = p % 4;
                if (n === "0") {
                    zeroCount++;
                } else {
                    if (zeroCount > 0) {
                        chineseStr += cnNums[0];
                    }
                    // 归零
                    zeroCount = 0;
                    chineseStr += cnNums[parseInt(n, 10)] + cnIntRadice[m];
                }
                if (m === 0 && zeroCount < 4) {
                    chineseStr += cnIntUnits[q];
                }
            }
            chineseStr += cnIntLast;
        }
        // 小数部分
        if (decimalNum !== "") {
            const decLen = decimalNum.length;
            for (let i = 0; i < decLen; i++) {
                const n = decimalNum.substr(i, 1);
                if (n !== "0") {
                    chineseStr += cnNums[Number(n)] + cnDecUnits[i];
                }
            }
        }
        if (chineseStr === "") {
            chineseStr += cnNums[0] + cnIntLast + cnInteger;
        } else if (decimalNum === "") {
            chineseStr += cnInteger;
        }
        return chineseStr;
    }

    function exportInfo(id) {

        $.ajax({
            method: "get",
            url: table.options.exportUrlInfo + "?id=" + id,
            xhrFields: {
                responseType: "arraybuffer",
            },
            success: function (res, _, xhr) {
                // let [, fileName] = xhr
                //     .getResponseHeader("Content-Disposition")
                //     .match(/filename=(.*)$/);
                var date = (new Date()).getTime();
                const blob = new Blob([res], {type: 'application/pdf'});
                var ifr = document.createElement('iframe');
                ifr.style.frameborder = 'no';
                ifr.style.display = 'none';
                ifr.style.pageBreakBefore = 'always';
                let name = "pdf" + date;
                ifr.setAttribute('id', name);
                ifr.setAttribute('name', name);

                ifr.src = window.URL.createObjectURL(blob);
                document.body.appendChild(ifr);
                var pdfObject = document.getElementById(name).contentWindow;

                pdfObject.print();

                window.URL.revokeObjectURL(ifr.src);


            },
        });

    }

</script>
</body>
</html>